<?php

namespace App\Http\Controllers\Marketing;


use App\Model\Marketing\Adimage;
use Illuminate\Http\Request;
use App\Model\System\Token;
use Laravel\Lumen\Routing\Controller as BaseController;

use App\Http\Controllers\MessagePacket;


class AdimageController extends BaseController
{
    use MessagePacket;


    function  __construct(){}


    /**
     * 获取单一图片
     * @param $id
     * @return packetMessage
     */
    function get($id)
    {
        $id = intval($id);
        $obj = Adimage::find($id);
        if(empty($obj)){
            return $this->fail($obj, '数据不存在');
        }
        return $this->success($obj);
    }


    /**
     * 批量查询
     * @param Request $request
     * @return packetMessage
     */
    function gets(Request $request)
    {
        $query =  Adimage::wherenotNull('id');
        if($request->has('status')){
            $status = $request->get('status');
            $query->where('status', $status);
        }
        $data = $query->get();

        if(empty($data)){
            return $this->fail($data, '数据不存在');
        }
        return $this->success($data);
    }


    /**
     * 添加一个
     * @param Request $request
     * @return packetMessage
     */
    function  create(Request $request)
    {
        $obj = new Adimage($request->all());
        $obj->created_by = Token::user()->userid;
        $obj->updated_by = Token::user()->userid;
        if($obj->save()){
            return $this->success($obj);
        }else{
            return $this->fail(false,'保存失败');
        }
    }


    /**
     * 更新某个
     * @param Request $request
     * @return packetMessage
     */
    function  update($id, Request $request)
    {
        $obj = Adimage::find($id);
        $obj->update($request->all());
        $obj->updated_by = Token::user()->userid;

        if($obj->save()){
            return $this->success($obj);
        }else{
            return $this->fail(false,'保存失败');
        }
    }

    /**
     * 禁用
     * @param $id
     * @return packetMessage
     */
    function  disable($id)
    {
        $obj = Adimage::find($id);
        $obj->status = 0;
        if($obj->save()){
            return $this->success(true);
        }else{
            return $this->fail(false, '失败');
        }

    }

    /**
     * 删除
     * @param $id
     * @return packetMessage
     */
    function  delete($id)
    {
        if(Adimage::destroy($id)){
            return $this->success(true);
        }else{
            return $this->fail(false,'删除失败');
        }

    }

}
